﻿using LuDragon.Common.Utils;
using LuDragon.IService.System;
using System.Web.Mvc;

namespace LuDragon.Web.Areas.Admin.Controllers
{
    /// <summary>
    /// 登录控制器
    /// </summary>
    public class LoginController : BaseController
    {
        private IUserService _userService;
        public LoginController(IUserService userService)
        {
            _userService = userService;
        }
        #region 视图        
        /// <summary>
        /// 登录
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
            return View();
        }
        #endregion
        #region 数据
        [HttpPost]
        public ActionResult Login(string userName, string password)
        {
            try
            {
                var admin = _userService.GetUser(userName, password);
                if (admin != null)
                {
                    //保存session cookie
                    _userService.SetLoginCookieSession(admin);
                    WriteLog("登录", Common.Enums.LogMode.Login);
                    return Success(admin);
                }
                //用户名或密码错误                        
                else
                {
                    throw new System.Exception("用户名或密码错误");
                }
            }
            catch (System.Exception e)
            {
                throw e;
            }
        }
        #endregion
    }
}